{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "047035d2",
   "metadata": {},
   "source": [
    "# PassTimingInstrument\n",
    "\n",
    "{class}`tvm.instrument.PassTimingInstrument` 是用 C++ 实现的创建 pass 时间计时的包装器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4087a9d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tvm\n",
    "import tvm.testing\n",
    "from tvm import relax, topi\n",
    "from tvm.script import ir as I, relax as R, tir as T\n",
    "\n",
    "def test():\n",
    "    bb = relax.BlockBuilder()\n",
    "    x = relax.Var(\"x\", R.Tensor([10, 20], \"float32\"))\n",
    "    with bb.function(\"main\", [x]):\n",
    "        with bb.dataflow():\n",
    "            lv0 = bb.emit_te(topi.add, x, relax.const(1, \"float32\"))\n",
    "            lv1 = bb.emit_te(topi.exp, lv0)\n",
    "            gv = bb.emit_output(bb.call_te(topi.squeeze, lv1))\n",
    "        bb.emit_func_output(gv)\n",
    "\n",
    "    return bb.get()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c924e310",
   "metadata": {},
   "outputs": [],
   "source": [
    "timing_inst = tvm.instrument.PassTimingInstrument()\n",
    "relax_mod = test()\n",
    "with tvm.transform.PassContext(instruments=[timing_inst]):\n",
    "    relax_mod = relax.transform.FuseOps()(relax_mod)\n",
    "    # before exiting the context, get profile results.\n",
    "    profiles = timing_inst.render()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "02662db3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FuseOps: 343056us [343056us] (100.00%; 100.00%)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(profiles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b36c8f1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py313",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
